//
//  RBCouponPayVC.m
//  zhongzhenglu
//
//  Created by mac on 2022/11/2.
//

#import "RBCouponPayVC.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RanTool.h"
#import "UIViewController+RBAdd.h"
#import "RBCouponPayResponse.h"
#import "RBAlipayUtils.h" //支付宝

@interface RBCouponPayVC ()

@property(nonatomic, strong)UIScrollView *scrollView;
@property(nonatomic, strong)NSMutableArray *payWayViewsArray;
@property(nonatomic, assign)NSInteger payIndex;

@property (nonatomic, strong) RBCouponPayResponse *response;

@end

@implementation RBCouponPayVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];

}

#pragma mark - Private

- (void)setup
{
    self.title = @"收银台";
    self.view.backgroundColor = [RBThemeConfig bgGrayColor];
    [self.scrollView rbSetContentWidth:kRBMainScreenWidth - Zoom(10)*2];
    [self.scrollView addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(10)];
        
        [view view_addView:UIView.new withHeight:Zoom(10) width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
        
        [view view_addView:[self infoViewWithTitle:@"订单编号" des:self.orderNum sType:0] withHeight:Zoom(36) width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
        [view view_addView:[self infoViewWithTitle:@"订单金额" des:self.price sType:1] withHeight:Zoom(36) width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
        
        [view view_addView:UIView.new withHeight:Zoom(10) width:kRBMainScreenWidth top:0 bottom:0 left:0 right:0 centerX:0];
        
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(10) bottom:0 left:0 right:0 centerX:0];
    [self.scrollView addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(10)];
        
        [view view_addView:({
            UILabel *view = [RanTool labelWithFontSize:Zoom(16) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:@"支付方式"];
            view;
        }) withHeight:Zoom(54) width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(12) right:0 centerX:0];
        
        for (NSInteger i = 0; i < 4; i++)
        {
            [view view_addView:[self payWayWithIndex:i] withHeight:Zoom(50) width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
        }
        
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(10) bottom:0 left:0 right:0 centerX:0];
    [self.scrollView addView:({
        UILabel *view = [RanTool labelWithFontSize:15 weight:0 color:[UIColor whiteColor] align:1 text:@"确认支付"];
        [view rbAddTapGesWithTarget:self action:@selector(payBtnTapped)];
        view.backgroundColor = [UIColor colorWithHexString:@"#518CFF"];
        [view rbSetCornerRadius:Zoom(22)];
        view;
    }) withHeight:Zoom(44) width:CGFLOAT_MIN top:Zoom(130) bottom:Zoom(50) left:0 right:0 centerX:0];
    
    [self selectPayViewAtIndex:0];
}

- (void)selectPayViewAtIndex: (NSInteger)index
{
    self.payIndex = index;
    for (UIView *itemView in self.payWayViewsArray)
    {
        UIImageView *imgView = (UIImageView *)[itemView viewWithTag:33];
        imgView.image = itemView.tag == index? [UIImage imageNamed:@"addressListDefault"]: [UIImage imageNamed:@"groupPayUnsel"];
    }
}

#pragma mark - Network

#pragma mark - Event

- (void)payWayViewTapped: (UITapGestureRecognizer *)ges
{
    [self selectPayViewAtIndex:ges.view.tag];
}

- (void)payBtnTapped
{
    __weak typeof(self) weak_self = self;
    if (self.payIndex == 1) {
        [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
            dicM[@"order_number"] = self.orderNum;
        } path:@"api/alipayapp_hu_coupon_buy" post:YES log:YES responseClass:RBCouponPayResponse.class desView:self.view showHud:YES showPlaceHolder:NO sucBlock:^(RBCouponPayResponse *data) {
            [weak_self alipayOrderWith:data];
        } failBlock:^(RBCouponPayResponse *data) {
            [weak_self.view showTip:data.message completion:nil];
        } reloadBlock:^{
        }];

        
    } else if(self.payIndex == 2) {
        [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
            dicM[@"order_number"] = self.orderNum;
        } path:@"hu_coupon_balance_pay" post:YES log:YES responseClass:RBResponse.class desView:self.view showHud:YES showPlaceHolder:NO sucBlock:^(id data) {
            [weak_self.view showTip:@"购买成功" completion:^{
                [weak_self.navigationController popViewControllerAnimated:YES];
            }];
        } failBlock:^(RBResponse *data) {
            [weak_self.view showTip:data.message completion:nil];
        } reloadBlock:nil];
    }
}
//支付宝支付
- (void)alipayOrderWith:(RBCouponPayResponse *)data {
    __weak typeof(self) weak_self = self;
    [RBAlipayUtils payWithOrder:data.row completion:^(BOOL succeed, NSString * _Nonnull message) {
        if (succeed) {
            [weak_self.view showTip:@"购买成功" completion:^{
                [weak_self.navigationController popViewControllerAnimated:YES];
            }];
        } else {
            [weak_self.view showTip:@"取消支付" completion:nil];
        }
    }];
}



#pragma mark - Setter

#pragma mark - Getter

- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        self.scrollView = ({
            UIScrollView *view = UIScrollView.new;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.bottom.mas_equalTo(0);
                make.left.mas_equalTo(Zoom(10));
                make.right.mas_equalTo(Zoom(-10));
            }];
            view;
        });
    }
    return _scrollView;
}

- (UIView *)infoViewWithTitle: (NSString *)title des: (NSString *)des sType: (NSInteger)sType
{
    UIView *view = UIView.new;
    
    UILabel *titleL = [RanTool labelWithFontSize:Zoom(16) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:title];
    [view addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Zoom(13));
        make.centerY.mas_equalTo(0);
    }];
    
    UILabel *desL = [RanTool labelWithFontSize:Zoom(14) weight:0 color:@"#969593" align:2 text:des];
    [view addSubview:desL];
    [desL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-Zoom(12));
        make.centerY.mas_equalTo(0);
    }];
    
    if(sType == 1)
    {
        desL.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
            [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:16] color:[RBThemeConfig textBlackColor] string:@"¥"];
            [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:22] color:[RBThemeConfig textBlackColor] string:des];
        }];
    }
    
    return view;
}

- (UIView *)payWayWithIndex: (NSInteger)index
{
    NSArray *imgs = @[
        @"groupPayWechat",
        @"groupPayAli",
        @"groupPayWallet",
        @"groupPayWallet",
    ];
    NSArray *names = @[
        @"微信",
        @"支付宝",
        @"余额支付",
        @"好友代付"
    ];
    UIView *view = UIView.new;
    [self.payWayViewsArray addObject:view];
    view.tag = index;
    [view rbAddTapGesWithTarget:self action:@selector(payWayViewTapped:)];
    
    UIImageView *imgView = [UIImageView.alloc initWithImage:[UIImage imageNamed:imgs[index]]];
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    [view addSubview:imgView];
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Zoom(12));
        make.width.height.mas_equalTo(Zoom(18));
        make.centerY.mas_equalTo(0);
    }];
    
    UILabel *nameL = [RanTool labelWithFontSize:Zoom(15) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:names[index]];
    [view addSubview:nameL];
    [nameL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(imgView.mas_right).offset(Zoom(9));
        make.centerY.mas_equalTo(0);
    }];
    
    UIImageView *selImgView = UIImageView.new;
    selImgView.tag = 33;
    [view addSubview:selImgView];
    [selImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-Zoom(12));
        make.width.height.mas_equalTo(Zoom(15));
    }];
    
    return view;
}

- (NSMutableArray *)payWayViewsArray
{
    if (!_payWayViewsArray)
    {
        self.payWayViewsArray = NSMutableArray.array;
    }
    return _payWayViewsArray;
}


@end
